import 'package:find_friend/common/alert_tool.dart';
import 'package:find_friend/common/common_widget.dart';
import 'package:find_friend/const/const.dart';
import 'package:find_friend/network/network.dart';
import 'package:find_friend/pages/mine/mine_xspace.dart';
import 'package:find_friend/pages/xspace/model/artclemodel.dart';
import 'package:flutter/material.dart';

import 'raise_animated.dart';
import 'xdiscuss_image.dart';

class XAllDiscussWidget extends StatefulWidget {

  final ReplyModel model;
  const XAllDiscussWidget({Key key, this.model}) : super(key: key);

  @override
  _XAllDiscussWidgetState createState() => _XAllDiscussWidgetState();
}

class _XAllDiscussWidgetState extends State<XAllDiscussWidget> with SingleTickerProviderStateMixin {

  Animation<double> _animation;
  Animation<double> _scaleAnimation;
  AnimationController _animationController;
  int upvotes = 0;
  @override
  void initState() {
    super.initState();

    _animationController = AnimationController(vsync: this, duration: const Duration(milliseconds: 300));
    CurvedAnimation _cureve = CurvedAnimation(parent: _animationController, curve: Curves.bounceIn);
    _animation = Tween<double>(begin: 0, end: 1).animate(_cureve);
    _scaleAnimation = Tween<double>(end: 0.0, begin: 1.0).animate(_cureve);

    if(_islikeFlag()) _animationController.forward(); 
    if(_isdislikeFlag()) _animationController.reverse(); 
  }

  @override
  void didUpdateWidget(XAllDiscussWidget oldWidget) {
    super.didUpdateWidget(oldWidget);
    // 列表刷新的时候会重新走一遍
    if(_islikeFlag()) _animationController.forward(); 
    if(_isdislikeFlag()) _animationController.reverse(); 
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  bool _isdislikeFlag() {
    return widget.model?.dissflag != null;
  }

  bool _islikeFlag() {
    return widget.model?.upvoteflag != null;
  }

  onLikeAction(bool islike){
    if(widget.model == null) return; 
    Map<String, dynamic> param = {};
    param["rid"] = widget.model.id;
    Networktool.request(API.upvote, params: param, method: islike ? NETWORKHTTPMethod.POST : NETWORKHTTPMethod.DELETE, success: (data){
      // AlertTool.showToast("点赞成功");
        if(islike){
          upvotes = widget.model.dissflag != null ? 2 : 1;
        } else {
          upvotes = widget.model.upvoteflag != null ? -2 : -1;
        }
        if(islike){
          widget.model.upvoteflag = 1;
          widget.model.dissflag = null;
        } else {
          widget.model.upvoteflag = null;
          widget.model.dissflag = 1;
        }
        setState(() {
          widget.model.upvotes += upvotes;
        });
        islike ? _animationController.forward() : _animationController.reverse();
    }
    ,fail: (msg) => AlertTool.showToast(msg)
    );
  }

  @override
  Widget build(BuildContext context) {
    final size = MediaQuery.of(context).size;
    final width = size.width * 0.5 / 2.0;
    return Container(
      margin: const EdgeInsets.symmetric(horizontal: 15),
      decoration: BoxDecoration(
        color: APP_scaffold_back_color,
        borderRadius: BorderRadius.circular(5)
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          InkWell(
            onTap: (){},
            child: Padding(
              padding: const EdgeInsets.only(left:6.0, top: 6, bottom: 10),
              child: Row(
                children: [
                  InkWell(
                    onTap: (){
                      Navigator.of(context).pushNamed("/minepreview", arguments: widget.model.mobile);
                    },
                    child: CustomCircleAvatar(size: 26, url: widget.model?.avatar ?? "",)
                  ),
                  SizedBox(width: 5),
                  Expanded(
                    child: Text(widget.model?.remarkname ?? "", style: instanceTextStyle(10, APP_font_black),)
                  ),
                  InkWell(
                    onTap: () {
                      onLikeAction(true);
                    },
                    child: Container(
                      height: 30,
                      width: 30,
                      alignment: Alignment.center,
                      child: RiseAnimatedWidget(
                        dissAnimation: _scaleAnimation, 
                        likeAnimation: _animation,
                        imgW: 20, 
                        iszan: true, 
                        value: _islikeFlag(),
                        fit: BoxFit.scaleDown,
                      ),
                    ),
                  ),
                  Container(
                    width: 60,
                    height: 30,
                    alignment: Alignment.center,
                    child: Text("${widget.model?.strUpvotes ?? 0}", style: instanceTextStyle(12, APP_font_black),)
                  ),
                  InkWell(
                    onTap: () {
                      onLikeAction(false);
                    },
                    child: Container(
                      height: 30,
                      width: 30,
                      alignment: Alignment.center,
                      child: RiseAnimatedWidget(
                        dissAnimation: _animation, 
                        likeAnimation: _scaleAnimation,
                        imgW: 20,  
                        iszan: false, 
                        value: _isdislikeFlag(),
                        fit: BoxFit.scaleDown,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Offstage(
            offstage: widget.model?.content == null,
            child: Padding(
              padding: const EdgeInsets.only(left: 10, right: 5),
              child: Text(widget.model?.content ?? "asdfasdf", style: instanceTextStyle(10, APP_font_black),),
            ),
          ),
          Offstage(
            offstage: widget.model?.photo == null,
            child: Padding(
              padding: EdgeInsets.only(left:15, top: 10),
              child: XDiscussImagesWidget(list: widget.model?.photo ?? [],)
            ),
          ),
          // 语音
          Offstage(
            offstage: widget.model?.voice == null,
            child: Padding(
              padding: const EdgeInsets.only(left: 10, top: 10),
              child: XspaceVoiceView(width: size.width * 0.45, voice: widget.model?.voice,),
            ),
          ),
          SizedBox(height: 10,)
        ],
      ),
    );
  }
}